package com.supermap.pisaclient.common;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.supermap.pisaclient.R;
import com.supermap.pisaclient.dynamic.DynamicFragment;
import com.supermap.pisaclient.map.MapFragment;
import com.supermap.pisaclient.mine.MineFragment;
import com.supermap.pisaclient.weather.WeatherFragment;
import com.supermap.util.base.BaseTabActivity;
import com.supermap.util.util.ResourcesUtil;
import com.supermap.util.util.SharedPreferencesUtil;
import com.supermap.util.view.ViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C) 2017 北京超图软件股份有限公司 版权所有
 * <p>
 * 功能描述：首页 作者：李会林
 * <p>
 * 修改人： 修改描述： 修改日期
 */
public class MainActivity extends BaseTabActivity {
    public static final String TAB_POSITION_KEY = "tabPosition";
    public static final String IS_POSITION0 = "isPosition0";
    public static long CLEAR_CACHE_INTERVAL_TIME = 7 * 24 * 60 * 60 * 1000;
    private static final long INTERVAL_EXIT_BACK_PRESSED = 2000;
    private boolean mIsFirst = true;
    private long mBackPressed;
    private String mCity;

    @Override
    public void init() {
        ViewUtils.inject(this);
        initView();
        addTabs();
        setShowNetDefault(false);
        SharedPreferencesUtil sp = SharedPreferencesUtil.getInstance(getApplicationContext());
        sp.putBoolean(IS_POSITION0, false);
    }

    private void initView() {
        setType(TYPE_MIDDLE_ACTION);
        setTabBgAlpha(242);
    }

    private void addTabs() {
        try {
            List<Class<? extends Fragment>> list = new ArrayList<Class<? extends Fragment>>();
            list.add(WeatherFragment.class);
            list.add(MapFragment.class);
            list.add(DynamicFragment.class);
            list.add(MineFragment.class);
            addViews(
                    new String[]{ResourcesUtil.getString(R.string.weather),
                            ResourcesUtil.getString(R.string.map),
                            ResourcesUtil.getString(R.string.dynamic),
                            ResourcesUtil.getString(R.string.mine)},
                    null, new int[]{R.drawable.can_see, R.drawable.can_see,
                            R.drawable.can_see, R.drawable.can_see},
                    list);
            hideMidBtn();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (intent != null) {
            int position = intent.getIntExtra(TAB_POSITION_KEY, -1);
            setCurrentTab(position);
            setIntent(intent);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferencesUtil sP = SharedPreferencesUtil.getInstance(getApplicationContext());
        boolean pos = sP.getBoolean(IS_POSITION0, false);
        if (getPos() != 0) {
            if (pos) {
                setCurrentTab(0);
            }
        }
        if (pos) {
            sP.putBoolean(IS_POSITION0, false);
        }
        onPageChanged(getPos());
        if (mIsFirst) {
            mIsFirst = false;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPageChanged(int position) {
        switch (position) {
            case 0:
                setTitle(R.string.weather);
                setLeftVisibility(View.GONE);
                Fragment user = getFragment(position);
                if (user != null && user.isAdded()) {
                    //((UserFragment) user).getUnReadCount();
                }
                needHeader(false);
                break;
            case 1:
                needHeader(true);
                setTitle(R.string.map);
                break;
            case 2:
                setTitle(R.string.dynamic);
                Fragment redpacket = getFragment(position);
                if (redpacket != null && redpacket.isAdded()) {
                }
                needHeader(true);
                break;
            case 3:
                setTitle(R.string.mine);
                Fragment music = getFragment(position);
                if (music != null && music.isAdded()) {
                }
                needHeader(true);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    private void backPressed() {
        if (mBackPressed + INTERVAL_EXIT_BACK_PRESSED > System.currentTimeMillis()) {
            clearExit();
            finish();
        } else {
            Toast.makeText(this, getString(R.string.exit_again_click), Toast.LENGTH_SHORT).show();
        }
        mBackPressed = System.currentTimeMillis();
    }

    @Override
    protected void setTabActivityBackground() {
        getBaseContent().setBackgroundColor(ResourcesUtil.getColor(R.color.common_grey_bg_color3));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/*
        if (requestCode == CityListSelectActivity.CITY_SELECT_RESULT_FRAG) {
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    Bundle bundle = data.getExtras();
                    CityInfoBean cityInfoBean = (CityInfoBean) bundle.getParcelable("cityinfo");
                    if (!TextUtils.isEmpty(cityInfoBean.getName())) {
                        WeatherFragment fragment1 = new WeatherFragment();
                        Bundle bundle1 = new Bundle();
                        bundle.putString("city_name", cityInfoBean.getName());
                        fragment1.setArguments(bundle);
                    }
                }
            }
        }
*/
        super.onActivityResult(requestCode, resultCode, data);
    }
}